package com.kgc.service.impl;

import com.kgc.dao.UserInfoDao;
import com.kgc.entity.UserInfo;
import com.kgc.entity.vo.UserInfoVo;
import com.kgc.service.UserInfoService;
import com.kgc.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired //从容器中拿出 UserInfoDao
    private UserInfoDao userInfoDao;

    /**
     * 登录的业务现实
     *
     * @param userName 用户名
     * @param userPwd  密码
     * @return 用户对象
     */
    @Override
    public UserInfo getUserInfoByUserNameAndUserPwd(String userName, String userPwd,String puserName,boolean isShow) {

        Map<String, Object> map = new HashMap<>();
        map.put("userName", userName);
        map.put("userPwd", userPwd);
        if (isShow){
            map.put("puserName",puserName);
            return userInfoDao.getUserInfoByUserNameAndUserPwdPuser(map);
        }
        return userInfoDao.getUserInfoByUseNameAndUserPwd(map);
    }

    @Override
    public int countUserInfoByUserName(String userName) {
        return userInfoDao.countUserInfoByUserName(userName);
    }

    @Override
    public Integer addUserInfo(UserInfo userInfo,Integer type) {
        if (type==null){
            userInfo.setId(1);
        }else {
            userInfo.setPId(2);
        }
        return userInfoDao.addUserInfo(userInfo);
    }

    @Override
    public List<UserInfoVo> getUserInfoAll(Integer index, Integer limit, Map<String, Object> map1) {
        Map<String, Object> map = new HashMap<>();
        map.put("index", index);
        map.put("pageSize", limit);
        map.put("userName", map1.get("username"));
        map.put("name", map1.get("name"));
        map.put("pId",map1.get("pId"));
        return userInfoDao.getUserInfoAll(map);
    }

    @Override
    public int countUserInfoAll(Map<String, Object> map1) {
        Map<String, Object> map = new HashMap<>();
        map.put("userName", map1.get("username"));
        map.put("name", map1.get("name"));
        map.put("pId",map1.get("pId"));
        return userInfoDao.countUserInfoAll(map);
    }

    @Override
    public int updateUserInfo(String field, String value, Integer id) {

        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        //当前这个switch 也可以选中用反射来处理
        switch (field) {
            case "userName":
                userInfo.setUserName(value);
                break;
            case "name":
                userInfo.setName(value);
                break;
            case "userPwd":
                userInfo.setUserPwd(value);
                break;
        }
        return userInfoDao.updateUserInfo(userInfo);
    }

    @Override
    public int delUserInfoById(Integer id) {
        return userInfoDao.delUserInfoById(id);
    }

    @Override
    public UserInfoVo getUserInfoById(Integer id) {

        UserInfoVo user=userInfoDao.getUserById(id);

        return user;
    }

    @Override
    public Integer updateUserInfo(UserInfo userInfo) {
        return userInfoDao.updateUserInfo(userInfo);
    }

    @Override
    public Integer addUserInfo(UserInfo userInfo) {
        return userInfoDao.addUserInfo(userInfo);
    }

    @Override
    public int updateUserMenu(String checkedIds, Integer userId) {
        userInfoDao.delMenuAll(userId);//根据RoleId清除子账户和菜单原有关系
        List<Integer> ids = JsonUtils.jsonToList(checkedIds, Integer.class);
        System.err.println(ids);
        int num = 0;
        for (Integer id : ids) {
            userInfoDao.addUserMenu(id, userId);//重新绑定用户和菜单
            num++;
        }
        return num;
    }
}
